package com.jeesite.modules.rt.service;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.rt.entity.RtDevice;
import com.jeesite.modules.rt.dao.RtDeviceDao;

/**
 * 路由器设备Service
 * @author 李寒冰
 * @version 2024-09-26
 */
@Service
public class RtDeviceService extends CrudService<RtDeviceDao, RtDevice> {





	/**
	 * JSON转设备实例
	 * @param jsonObject
	 */
	public void jsonToDevice(JSONObject jsonObject){
		String sn = jsonObject.getString("sn");
		RtDevice device= getDeviceBySn(sn);
		if(device==null){
			device = new RtDevice();
			//生成序列号
			long l = System.currentTimeMillis();
			String xuliehao ;
			boolean ischong = true;
			 do{
				 xuliehao = new Long(l/1000)+ StringUtils.getRandomNum(3);
				 if(getDeviceByXuliehao(xuliehao)==null){
					 ischong = false;
				 }
			 }while(ischong);
			 
			device.setXuliehao(xuliehao);
		}
		//设备号
		device.setSn(sn);
		//设备类型
		device.setPipeType(jsonObject.getString("pipeType"));
		//设备架构
		device.setModel(jsonObject.getInteger("model").toString());
		//在线标志
		device.setOnline(jsonObject.getLong("onlineCode").toString());
		//备注
		device.setRemark(jsonObject.getString("remark"));
		//是否绑定用户（1：绑定，0：未绑定）
		device.setBind(jsonObject.getString("bind"));
		//设备创建时间
		device.setCreateTime(new Date(jsonObject.getLong("createTime")));
		//刷洗时间
		device.setRefreshTime(new Date());


		save(device);
	}

	@Transactional(readOnly = true)
	public RtDevice getDeviceBySn(String sn){
		RtDevice device = new RtDevice();
		device.setSn(sn);
		List<RtDevice> list = dao.findList(device);
		if(list != null && list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}

	@Transactional(readOnly = true)
	public RtDevice getDeviceByXuliehao(String xuliehao){
		RtDevice device = new RtDevice();
		device.setXuliehao(xuliehao);
		List<RtDevice> list = dao.findList(device);
		if(list != null && list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * 获取单条数据
	 * @param rtDevice
	 * @return
	 */
	@Override
	public RtDevice get(RtDevice rtDevice) {
		return super.get(rtDevice);
	}
	
	/**
	 * 查询分页数据
	 * @param rtDevice 查询条件
	 * @param rtDevice page 分页对象
	 * @return
	 */
	@Override
	public Page<RtDevice> findPage(RtDevice rtDevice) {
		return super.findPage(rtDevice);
	}
	
	/**
	 * 查询列表数据
	 * @param rtDevice
	 * @return
	 */
	@Override
	public List<RtDevice> findList(RtDevice rtDevice) {
		return super.findList(rtDevice);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param rtDevice
	 */
	@Override
	@Transactional
	public void save(RtDevice rtDevice) {

		super.save(rtDevice);
	}
	
	/**
	 * 更新状态
	 * @param rtDevice
	 */
	@Override
	@Transactional
	public void updateStatus(RtDevice rtDevice) {
		super.updateStatus(rtDevice);
	}
	
	/**
	 * 删除数据
	 * @param rtDevice
	 */
	@Override
	@Transactional
	public void delete(RtDevice rtDevice) {
		super.delete(rtDevice);
	}
	
}